<?php

namespace pmvc;

use pmvc\ClassLoader;

use Exception;

/**
 * Basic implementation of the {@link ClassLoader}
 * interface that uses directories as classpath
 * roots for loading classes.
 */
class FileSystemClassLoader
	extends AbstractClassLoader {

	private $classpath = Array();

	/**
	 * Creates the {@link FileSystemClassLoader} and configures
	 * it for loading classes from the given classpath.
	 * @param $classpath an array of class path elements
	 */
	public function __construct($classpath=Array()) {
		if (!is_array($classpath)) {
			$classpath = Array($classpath);
		}
		foreach ($classpath as $cp) {
			$this->addToClassPath($cp);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	protected function getClasspath() {
		return $this->classpath;
	}

	/**
	 * Adds an entry to the class path.
	 * @param string $path the path
	 */
	function addToClassPath($path) {
		if (!file_exists($path)) {
			throw new Exception("Path doesn't exist: $path");
		} else if (!is_dir($path)) {
			throw new Exception("Path is not a directory: $path");
		}
		$path = str_replace('\\', '/', $path);
		if ($this->shouldUseRealpath()) {
			$path = realpath($path);
		}
		array_push($this->classpath, $path);
	}
}

?>